<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
<div class="contain">
    <?php if (!empty($lists)) : ?>
	<?php
		/*
		 * pagination
		 */
		 $pages = $this->Paginator->counter((array('format' => '%pages%')));
         $pageCurrent = $this->Paginator->counter((array('format' => '%page%')));
	?>
    <?php echo $this->Form->create('Todo', array('method' => 'post', 'id' => 'TodoForm')); ?>
    <div class="contain-sub">
        <div id="message-box">
            <?php echo $this->Session->flash(); ?>
        </div>
        <div class="div-button"> <!-- button -->
            <input type="button" value="すべて選択" id="select-all-1">
            <input type="button" value="すべて解除" id="unselect-all-1">
            <?php if ($this->action == 'complete'): ?>
            <input type="button" value="未完" id="uncomplete-1">
			<?php else: ?>
			<input type="button" value="完了" id="complete-1">
			<?php endif; ?>
            <input type="button" value="削除" id="delete-1">
        </div> <!-- end button -->
        <div class="main">
            <div class="pagging">
                <span>
                    <?php
                        echo $this->Paginator->counter(array(
                            'format' => '全{:count}件中  {:start}～{:end}件表示'
                        ));
                    ?>
                </span>
                <select class="combo-pagging">
					<?php
                	   if ($pages >= 1) {
                	   		for($i = 1; $i <= $pages; $i++) {
                	   			$selected = '';
                	   			if($pageCurrent == $i) {
                	   				$selected = 'selected="selected"';
                	   			}
                	   			echo '<option value="'.$i.'" '.$selected.'>'.$i.'/'.$pages.'ページ'.'</option>';
                	   		}
                	   }
                ?>
                </select>
                <?php if ($pages > 1) : ?>
                <span class="page">
                    <?php
						if ($pageCurrent != 1) :
							echo '<a href="'.Router::url(array('controller' => 'pages', 'action' => $this->action, 'page' => $pageCurrent - 1)).'"><</a> ';
						endif;
                    ?>
                    <?php
						for($i = 1; $i <= $pages; $i++) :
							if (abs($i - $pageCurrent) >=0 && (abs($i - $pageCurrent)) <= 2) :
								if ($i == $pageCurrent) :
									echo '<a href="'.Router::url(array('controller' => 'pages', 'action' => $this->action, 'page' => $i)).'" class="active">'.$i.'</a> ';
								else:
									echo '<a href="'.Router::url(array('controller' => 'pages', 'action' => $this->action, 'page' => $i)).'" >'.$i.'</a> ';
								endif;
							endif;
						endfor;
					?>
                    <?php
						if ($pageCurrent != $pages) :
							echo ' <a href="'.Router::url(array('controller' => 'pages', 'action' => $this->action, 'page' => $pageCurrent + 1)).'">></a> ';
						endif;
                    ?>
                </span>
                <?php
                    endif;
                ?>
            </div>
                <div class="data" id="data">
                    <table>
                        <tr>
                            <th style="width: 50px;"><div>選択</div></th>
                            <th style="width: 400px;"><div>タイトル</div></th>
                            <th style="width: 300px;"><div>期限</div></th>
                            <th style="width: 100px;"><div>優先度</div></th>
                            <th><div>コメント</div></th>
                        </tr>
                        <?php foreach ($lists as $list) : ?>
                        <?php
                            // check expired end date
                            if (strtotime($list['Todo']['end_date']) < time() && !$isCompleted) :
                        ?>
                            <tr id="todo" class="red">
                        <?php
                            else :
                        ?>
                            <tr id="todo">
                        <?php
                            endif;
                        ?>
                                <td class='c'>
                                    <input type="checkbox" name="ids[]" value="<?php echo  $list['Todo']['id']; ?>" >
                                </td>
                                <td class='l'>
                                <?php
                                	$note = $list['Todo']['note'];
                                	if (empty($note)) $note = "なし";
                                    echo $this->Html->link($list['Todo']['title'].'<span>'.wordwrap($note).'</span>',
                                            array('controller' => 'pages', 'action' => 'edit', $list['Todo']['id']),
                                            array('class'=> 'tooltip','escape' => false)
                                    );
                                ?>
                                </td>
                                <td class='l'><?php echo date('Y-m-d H:i',strtotime($list['Todo']['end_date'])); ?></td>
                                <td class='c'>
                                    <?php
                                        switch ($list['Todo']['priority']) :
                                            case '2':
                                                echo '中';
                                                break;
                                            case '3':
                                                echo '低';
                                                break;
                                            default:
                                                echo '高';
                                        endswitch;
                                    ?>

                                </td>
                                <td class='c'><?php echo ($list['Todo']['comment']) ? $list['Todo']['comment'] : '-' ; ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </table>
                </div>
                <div class="pagging">
                    <span>
                        <?php
                            echo $this->Paginator->counter(array(
                                'format' => '全{:count}件中  {:start}～{:end}件表示'
                            ));
                        ?>
                    </span>
                    <select class="combo-pagging">
		                <?php
		                	   if ($pages >= 1) {
		                	   		for($i = 1; $i <= $pages; $i++) {
		                	   			$selected = '';
		                	   			if($pageCurrent == $i) {
		                	   				$selected = 'selected="selected"';
		                	   			}
		                	   			echo '<option value="'.$i.'" '.$selected.'>'.$i.'/'.$pages.'ページ'.'</option>';
		                	   		}
		                	   }
		                ?>
	                </select>
                    <?php if ($pages > 1) : ?>
                    <span class="page">
                    <?php
						if ($pageCurrent != 1) :
							echo '<a href="'.Router::url(array('controller' => 'pages', 'action' => $this->action, 'page' => $pageCurrent - 1)).'"><</a> ';
						endif;
                    ?>
                    <?php
						for($i = 1; $i <= $pages; $i++) :
							if (abs($i - $pageCurrent) >=0 && (abs($i - $pageCurrent)) <= 2) :
								if ($i == $pageCurrent) :
									echo '<a href="'.Router::url(array('controller' => 'pages', 'action' => $this->action, 'page' => $i)).'" class="active">'.$i.'</a> ';
								else:
									echo '<a href="'.Router::url(array('controller' => 'pages', 'action' => $this->action, 'page' => $i)).'" >'.$i.'</a> ';
								endif;
							endif;
						endfor;
                    ?>
                    <?php
						if ($pageCurrent != $pages) :
							echo ' <a href="'.Router::url(array('controller' => 'pages', 'action' => $this->action, 'page' => $pageCurrent + 1)).'">></a> ';
						endif;
                    ?>
					</span>
					<?php endif; ?>
                </div>
        </div>
        <input type="hidden" name="update_type" id="update_type">
        <div class="div-button"> <!-- button -->
            <input type="button" value="すべて選択" id="select-all-2">
            <input type="button" value="すべて解除" id="unselect-all-2">
			<?php if ($this->action == 'complete'): ?>
            <input type="button" value="未完" id="uncomplete-2">
			<?php else: ?>
			<input type="button" value="完了" id="complete-2">
			<?php endif; ?>
            <input type="button" value="削除" id="delete-2">
        </div> <!-- end button -->
    </div>
    <?php echo $this->Form->end(); ?>
    <?php
        else:
            echo '<div class="contain-sub">';
            echo '検索結果が0件です。';
            echo '</div>';
        endif;
    ?>
<script>
    jQuery(document).ready(function(){
    	    $("#data input[type=checkbox]").click(function(){
    	    	jQuery('#message-box').html("");
        	});
            // check all
            jQuery('#select-all-1').click(function(){
                 $("#data input[type=checkbox]").attr('checked','checked');
                 jQuery('#message-box').html("");
            });
            jQuery('#select-all-2').click(function(){
                 $("#data input[type=checkbox]").attr('checked','checked');
                 jQuery('#message-box').html("");
            });
            /***/
            // uncheck
            jQuery('#unselect-all-1').click(function(){
                 $("#data input[type=checkbox]").removeAttr('checked');
                 jQuery('#message-box').html("");
            });
            jQuery('#unselect-all-2').click(function(){
                 $("#data input[type=checkbox]").removeAttr('checked');
                 jQuery('#message-box').html("");
            });
            // delete action

            // ToDoリストscreen TD_0100
            // action 完了header
            jQuery('#complete-1').click(function(){
                $("#update_type").val('update_complete');
                $("#TodoForm").submit();
            });
            // action 完了 footer
            jQuery('#complete-2').click(function(){
                $("#update_type").val('update_complete');
                $("#TodoForm").submit();
        	});
            /**********************************************************/
            // 完了creen TD_0200
            // action 未完  header
            jQuery('#uncomplete-1').click(function(){
                $("#update_type").val('update_uncomplete');
                $("#TodoForm").submit();
            });
         	// action 未完  footer
			jQuery('#uncomplete-2').click(function(){
                $("#update_type").val('update_uncomplete');
                $("#TodoForm").submit();
            });
			/********************************************************/
			// action 削除 header
            jQuery('#delete-1').click(function(){
                $("#update_type").val('delete');
                $("#TodoForm").submit();
        	});
         	// action 削除 footer
            jQuery('#delete-2').click(function(){
                $("#update_type").val('delete');
                $("#TodoForm").submit();
        	});
        	//
        	jQuery('.combo-pagging').change(function(){
        		location.href = "<?php echo Router::url(array('controller' => 'pages', 'action' => $this->action, 'page' =>''));?>" + jQuery(this).val();
        	});
    });
</script>